package com.zycfc.zsf.boot.distributedlock.configuration;

import org.springframework.boot.context.properties.*;
import org.springframework.boot.autoconfigure.*;
import com.zycfc.zsf.boot.redis.autoconfigure.*;
import com.zycfc.zsf.boot.redis.*;
import org.springframework.beans.factory.annotation.*;
import com.zycfc.zsf.boot.distributedlock.*;
import org.springframework.context.annotation.*;
import org.springframework.boot.autoconfigure.condition.*;
import org.slf4j.*;

@Configuration
@EnableConfigurationProperties({ DistributedLockProperties.class })
@AutoConfigureAfter({ RedisOperaterAutoConfigure.class })
public class DistributedLockConfiguration
{
    private static final Logger log;
    @Autowired
    IRedisOperater redisOperater;
    
    @Bean
    @ConditionalOnMissingBean({ IDistributedLock.class })
    public IDistributedLock distributedLock(final DistributedLockProperties config) {
        IDistributedLock lock = null;
        if ("zk".equals(config.getMode())) {
            lock = new DistributedZkLock(config.getZkServers(), config.getApplicationName(), 0);
        }
        else {
            lock = new DistributedRedisLock(config.getApplicationName(), config.getNamespace(), null, this.redisOperater);
        }
        DistributedLockConfiguration.log.info("^_^\u5206\u5e03\u5f0f\u9501\u52a0\u8f7d\u6210\u529f^_^");
        return lock;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DistributedLockConfiguration.class);
    }
}
